#include "day.h"
#include "ui_day.h"

#include <QAbstractButton>
#include <QMessageBox>
#include <QPaintEvent>
#include <QPainter>
#include <QPushButton>
#include <QTime>

Day::Day(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Day)
{
    ui->setupUi(this);
    this->setGeometry(QRect(350,180,850,550));
    setFixedSize(850,550);

    QPushButton *btn21=new QPushButton("Back",this);
    btn21->resize(160,40);
    btn21->move(670,10);
    connect(btn21,&QPushButton::clicked,this,[=](){
       this->close();
    });

    menu1=new QMenu("New Game",this);
    menu2=new QMenu("Degree",this);

    ui->menuBar->addMenu(menu1);
    ui->menuBar->addMenu(menu2);
    connect(menu1,&QMenu::aboutToShow,this,&Day::slot_newGame);

    easy=new QAction("easy",this);
    normal=new QAction("normal",this);
    hard=new QAction("hard",this);
    self=new QAction("self define",this);

    menu2->addAction(easy);
    menu2->addAction(normal);
    menu2->addAction(hard);
    menu2->addAction(self);

    connect(easy,SIGNAL(triggered()),this,SLOT(slot_level(1)));
    connect(normal,SIGNAL(triggered()),this,SLOT(slot_level(2)));
    connect(hard,SIGNAL(triggered()),this,SLOT(slot_level(3)));
    connect(self,SIGNAL(triggered()),this,SLOT(slot_level(4)));

    dialogSelf=new Dialog(this);
    connect(dialogSelf,SIGNAL(dia_test()),this,SLOT(slot_level_self_get()));

    flagImage= QPixmap(":/images/images/4.png");
    bombImage=QPixmap(":/images/images/1.png");

    rows = 9;
    cols = 9;
    mine = 10;
    newGame();
}

void Day::initItems()
{
    items.clear();
    for(int i=0;i<mine;i++)
    {
        srand(QTime::currentTime().msec());
        int x = rand()%cols;
        int y = rand()%rows;

        while(Mines.contains(QPoint(x,y)))
        {
            x = rand()%cols;
            y = rand()%rows;
        }
        Mines.append(QPoint(x,y));
    }//完成雷的初始化

    for(int i=0;i<cols;i++)
    {
        for(int j=0;j<rows;j++)
        {
            if(items[i][j]->isMine)
            {
                continue;
            }
            int nCountMines=0;
            for(int m=-1;m<=1;m++)
            {
                for(int n=-1;n<=1;n++)
                {
                    if(m==0&&n==0)
                    {
                        continue;
                    }
                    QPoint ptNew=QPoint(i+m,j+n);
                    if(items[i+m][j+n]->isMine)
                    {
                        nCountMines++;
                    }
                }
            }items[i][j]->number=nCountMines;
        }
    }//计算没有雷的格子周围雷的个数
}
void Day::releaseItems()
{
    for(int i=0;i<items.size();i++)
    {
        for(int j=0;j<items[i].size();j++)
        {
            if(items[i][j]!=nullptr)
            {
                delete items[i][j];//不空就删除
                items[i][j]=nullptr;
            }
        }
    }
    items.clear();
}

void Day::newGame()
{
    m_bGameFail = false;
    resize(200 + cols*30,200 + rows*30);
    releaseItems();
    initItems();
}

void Day::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,850,550,QPixmap(":/images/images/11.jpg"));//作出背景画面
    painter.setPen(QPen(QColor(Qt::black),2));

    for(int i=0; i<cols; i++)
    {
        for(int j=0; j<rows; j++)
        {
            drawItem(painter,items[i][j]);//调用drawItem函数
        }
    }
    update();
}
void Day::drawItem(QPainter &painter, Item *pItem)
{
    if(pItem->isMarked)
    {
        if (m_bGameFail)
        {
            //游戏结束，显示雷
            QRect rcSrc(0,0,flagImage.width(),flagImage.height());
            QRect rcTarget(100 + pItem->m_pos.x()*30 + 2,100 + pItem->m_pos.y()*30 + 2,30-4,30-4);
            painter.drawPixmap(rcTarget,bombImage,rcSrc);//此时，雷已经画好
        }
        else
        {
            //游戏没结束，显示为旗子
            QRect rcSrc(0,0,flagImage.width(),flagImage.height());
            QRect rcTarget(100 + pItem->m_pos.x()*30 + 2,100 + pItem->m_pos.y()*30 + 2,30-4,30-4);
            painter.drawPixmap(rcTarget,flagImage,rcSrc);//此时，旗子已经画好
        }
        painter.setBrush(Qt::transparent);
        painter.drawRect( 100 + pItem->m_pos.x()*30,100 + pItem->m_pos.y()*30,30,30);

        return;
    }
    else if (pItem->isOpen)
    {
        if(pItem->number == 0)
            //如果为0 则为白色
        {
            painter.setBrush(Qt::white);//这时候白色的底色也没画好
        }
        else
        {
            //设置字体和其颜色
            QFont font;
            font.setPointSize(20);
            font.setFamily(("msyh"));
            font.setBold(true);

            painter.setBrush(Qt::white);
            painter.drawRect( 100 + pItem->m_pos.x()*30,
                              100 + pItem->m_pos.y()*30,30,30);
            //此时已经把有数字的方块的白色底色绘制完成。

            //开始写字
            painter.setBrush(Qt::black);
            painter.setFont(font);
            painter.drawText( 100 + pItem->m_pos.x()*30  + 8,
                              100 + pItem->m_pos.y()*30 + 26,QString("%1").arg(pItem->number));//此时文字也已经写毕
            return;
        }
    }
    else
    {
        painter.setBrush(Qt::yellow);
    }
    painter.drawRect( 100 + pItem->m_pos.x()*30,100 + pItem->m_pos.y()*30,30,30);
    //这一句既填充了黄色方块，又绘制了黑线，还填充了白色底色

}


void Day::slot_level(int a)
{
    switch(a)
    {
    case 1:
        rows = 9;
        cols = 9;
        mine = 10;
        newGame();break;
    case 2:
        rows = 16;
        cols = 16;
        mine = 40;
        newGame();break;
    case 3:
        rows = 16;
        cols = 30;
        mine = 99;
        newGame();break;
    case 4:
        dialogSelf->show();
        break;
    default:
        break;
    }
}

void Day::slot_level_self_get()
{
    rows=dialogSelf->getRows();
    cols=dialogSelf->getCols();
    mine=dialogSelf->getMine();
    newGame();
}

Day::~Day()
{
    delete ui;
}
